import React, { Component } from 'react';
import { Icon, Input, Button, Table, message } from 'antd';
import { withRouter } from 'react-router';
import axios from 'axios';
import './index.less';
const Search = Input.Search;
const columns = [
  {
    title: '用户名',
    dataIndex: 'username',
    key: 'username',
  }, {
    title: '职责',
    dataIndex: 'type',
    key: 'type',
  }, {
    title: '电话',
    dataIndex: 'telPhone',
    key: 'telPhone',
  }, {
    title: '生日',
    dataIndex: 'birthday',
    key: 'birthday',
  }, {
    title: '邮箱',
    dataIndex: 'email',
    key: 'email',
  }, {
    title: '状态',
    dataIndex: 'status',
    key: 'status',
  }, {
    title: '登录次数',
    dataIndex: 'attr6',
    key: 'attr6',
  }, {
    title: '上次登录时间',
    dataIndex: 'attr3',
    key: 'attr3',
  }, {
    title: '注册时间',
    dataIndex: 'crtTime',
    key: 'crtTime',
  },
];


class MemberUser extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      pagination: {
        current: 1,
        defaultCurrent: 1,
        total: 10,
        onChange: this.pageChange
      },
      SearchName: ''
    };
  }
  pageChange = (page) => {
    const $this = this;
    const Token = sessionStorage.getItem('token');
    $this.state.pagination.current = page;
    axios({
      method: 'get',
      url: '/api/admin/user/queryUserByUsername',
      headers: {
        Authorization: Token
      },
      params: {
        page: page,
        username: $this.state.SearchName
      }
    })
      .then(function (response) {
        $this.state.pagination.total = response.data.data.total;
        $this.setState({
          data: response.data.data.rows
        });
      })
      .catch(function (error) {
        message.error('请求失败，请稍后重试');
      });
  }
  getUserList = () => {
    const $this = this;
    const Token = sessionStorage.getItem('token');
    axios({
      method: 'get',
      url: '/api/admin/user/queryUserByUsername',
      headers: {
        Authorization: Token
      }
    })
      .then(function (response) {
        $this.state.pagination.total = response.data.data.total;
        $this.setState({
          data: response.data.data.rows
        });
      })
      .catch(function (error) {
        message.error('请求失败，请稍后重试');
      });
  }
  // 条件搜索
  searchUser = (value) => {
    const $this = this;
    const Token = sessionStorage.getItem('token');
    $this.state.SearchName = value;
    $this.state.pagination.current = 1;
    axios({
      method: 'get',
      url: '/api/admin/user/queryUserByUsername',
      headers: {
        Authorization: Token
      },
      params: {
        username: value
      }
    }).then(function (response) {
      $this.state.pagination.total = response.data.data.total;
      $this.setState({
        data: response.data.data.rows
      });
      // console.log(response.data)
    }).catch(function (error) {
      message.error('请求失败，请稍后重试');
    });

  }
  componentWillMount() {
    this.getUserList();

  }
  render() {
    const { match, location, history } = this.props;
    return (
      <div>
        <div className="index-buttons">
          <Search placeholder="用户名" onSearch={value => this.searchUser(value)} enterButton size="large" style={{ width: 260 }} />
        </div>
        <Table columns={columns} dataSource={this.state.data} rowKey="id" bordered pagination={this.state.pagination} />
      </div>


    );
  }
}

export default withRouter(MemberUser);
